package com.zhuanzhuan.hero.user.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.zhuanzhuan.hero.user.helper.ZzLockHelper;
import com.zhuanzhuan.hero.user.service.HeroLabelInfoService;
import com.zhuanzhuan.zzlock.lock.ZZEtcdLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName PullPanguBrandLabelsJob
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/26 10:30
 */
@Component
@Slf4j
public class PullPanguBrandLabelsJob {

    @Resource
    private HeroLabelInfoService labelInfoService;

    @Resource
    private ZzLockHelper lockHelper;

    @XxlJob("pullPanguBrandLabels")
    public ReturnT<String> pullPanguBrandLabels(String param) {
        log.info("pullPanguBrandLabels start");
        try (ZZEtcdLock lock = lockHelper.getLock("sync_pangu_brand_labels")) {
            if (!lock.tryLock()) {
                log.info("抢锁失败 param={}", param);
                XxlJobLogger.log("抢锁失败 param={}", param);
                return ReturnT.FAIL;
            }
            labelInfoService.pullPanguBrandLabels();
        } catch (Exception e) {
            log.error("pullPanguBrandLabels失败", e);
            XxlJobLogger.log("pullPanguBrandLabels执行异常");
            return ReturnT.FAIL;
        }

        return ReturnT.SUCCESS;
    }
}
